<?php
    include '../connectdb.php';
    if (isset($_GET['del'])) {
        $q="delete from home_stay_room where home_stay_room_id=".$_GET["home_stay_room_id"];
        mysqli_query($link, $q);
        header('Location: main.php?homestay_room=true');
        die;
    }
    if (isset($_POST['action'])) {
        $q = "home_stay_room set home_stay_room_name='".$_POST['name']."', home_stay_id=".$_POST['home_stay'].", ";
        $q.= "home_stay_room_detail='".$_POST['detail']."', price_per_night=".$_POST['price'].", max=".$_POST['max']."";
        if ($_POST['action'] == 'add') {
            $q = "insert into ".$q;
            $qq = "select count(max) as n from home_stay_room where home_stay_id=".$_POST['home_stay'];
            $res = mysqli_query($link, $qq);
            $row = mysqli_fetch_array($res);
            $_id = $_POST['home_stay'].sprintf('%02d', ($row['n']+1));
            echo $q.= ", home_stay_room_id=".$_id;
        } else { // edit
            $q = "update ".$q." where home_stay_room_id=".$_POST["home_stay_room_id"];
        }
        mysqli_query($link, $q);
    }
    if (isset($_GET["add"]) || isset($_GET["edit"])) {
        if (isset($_GET["edit"])) {
            $q = "select * from home_stay_room where home_stay_room_id=".$_GET["home_stay_room_id"];
            $result = mysqli_query($link, $q);
            $row = mysqli_fetch_array($result);
            $url = "main.php?homestay_room=true";//&edit=true&customer_id=".$_GET["customer_id"];
            $action = 'edit';
        } else {
            $url = "main.php?homestay_room=true";//&add=true";
            $action = 'add';
        }
?>
<form action="<?php echo $url ?>" method="POST">
<input type="hidden" name="action" value="<?php echo $action ?>" />
<input type="hidden" name="home_stay_room_id" value="<?php echo $_GET["home_stay_room_id"] ?>" />
<table border="1" width="764">
    <tr>
        <td colspan="2" align="center"><h3>Add/Edit Room</h3></td>
    </tr>
    <tr>
        <td>home stay</td>
        <td>
            <select name="home_stay">
                <?php
                    $q = "select * from home_stay order by home_stay_id asc";
                    $res = mysqli_query($link, $q);
                    while ($row2 = mysqli_fetch_array($res)) {
                        if (in_array($row2['home_stay_id'], $_SESSION['permission'])) {
                            $selected = ($row2["home_stay_id"] == $row2["home_stay_id"]) ? "selected" : "";
                ?>
                    <option value="<?php echo $row2["home_stay_id"] ?>" <?php echo $selected ?>><?php echo $row2["home_stay_name"] ?></option>
                <?php
                        }
                    }
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td>name</td>
        <td><input type="text" name="name" value="<?php echo $row["home_stay_room_name"] ?>" /></td>
    </tr>
    <tr>
        <td>detail</td>
        <td><textarea name="detail"><?php echo $row['home_stay_room_detail'] ?></textarea></td>
    </tr>
    <tr>
        <td>price</td>
        <td><input type="text" name="price" value="<?php echo $row["price_per_night"] ?>" /></td>
    </tr>
    <tr>
        <td>max person</td>
        <td><input type="text" name="max" value="<?php echo $row["max"] ?>" /></td>
    </tr>
    <tr>
        <td>picture</td>
        <td>
        <?php
            if ($row['home_stay_room_picture'] != '') {
        ?>
            <img src="../<?php echo $row['home_stay_room_picture'] ?>" />
        <?php
            }
        ?>
            <br />
            <input type="file" name="picture">
        </td>
    </tr>    
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></td>
    </tr>    
</table>
</form>
<?php
    } else {
?>
<script type="text/javascript">
    function confirm_delete (url) {
        if (confirm("โปรดยืนยันการลบข้อมูล")) {
            location.href = url;
        }
    }
</script>
<table border="1" width="764">
    <tr>
        <td colspan="7" align="center"><h3>Home_Stay_Room</h3></td>
    </tr>
    <tr>
        <td colspan="6" align="left">
            <select id="home_stay_name" name="home_stay_name" style="width :150px" onChange="window.location.href='main.php?homestay_room=true&home_stay_id='+this.value;">
                <?php 
                    $h_id = $_GET['home_stay_id'];
                    $q = "select * from home_stay";
                    $result = mysqli_query($link, $q);
                    while ($row = mysqli_fetch_array($result))
                    {
                        if (in_array($row['home_stay_id'], $_SESSION['permission']) || $row['home_stay_id'] == 0) {
                            $selected = ($h_id == $row['home_stay_id']) ? ' selected' : '';
                            echo '<option value='.$row['home_stay_id'].$selected.'>'.$row['home_stay_name'].'</option>';
                        }
                    }
                ?>
            </select>
        </td>
        <td align="right"><a href="main.php?homestay_room=true&add=true">+Add</a></td>
    </tr>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>detail</td>
        <td>price</td>
        <td>max</td>
        <td>edit</td>
        <td>delete</td>
    </tr>
    <?php
        $q = "select * from home_stay_room ";
        if ($h_id != 0) {
            $q.= " where home_stay_id=".$h_id;
        }
        else{
            $q.= " WHERE home_stay_id IN(".  implode(',',$_SESSION['permission'] ).")";
        }
        $result = mysqli_query($link, $q);
        while ($row = mysqli_fetch_array($result)){ 
    ?>
    <tr>
        <td><?php echo $row['home_stay_room_id'];?></td>
        <td><?php echo $row['home_stay_room_name'];?></td>
        <td><?php echo nl2br($row['home_stay_room_detail']);?></td>
        <td><?php echo $row['price_per_night'];?></td>
        <td><?php echo $row['max'];?></td>
        <td><a href="main.php?homestay_room=true&home_stay_room_id=<?php echo $row['home_stay_room_id'] ?>&edit=true">edit</a></td>
        <td><a href="javascript:confirm_delete('main.php?homestay_room=true&home_stay_room_id=<?php echo $row['home_stay_room_id'] ?>&del=true')">delete</a></td>
    </tr>
    <?php
        }
    ?>
</table>
<?php
    }
?>